#include "d_l_link_list.h"
// 创建带表头的双向循环链表
typedef struct head
{
    LNode head;
} head;
// 创建
linkList create_link_list(linkList head)
{
    head = (LNode *)malloc(sizeof(LNode));
    if (head == NULL)
    {
        printf("分配内存失败");
    }
    memset(head, 0, sizeof(LNode));
    head->next = head->prior = head;
    head->data = 0;
    printf("创建成功\n");
    return head;
}
// 查找
linkList list_find(linkList L, int pos)
{
    if (L == NULL)
    {
        printf("链表为空\n");
    }
    linkList oneNode = L->next;
    int i = 1;
    for (i = 1; i < pos; i++)
    {
        oneNode = oneNode->next;
    }
    return oneNode;
}
// 长度
int list_length(linkList L)
{
    if (L == NULL)
    {
        printf("链表为空\n");
        return -1;
    }
    linkList oneNode = L->next;
    int i = 1;
    while (oneNode->next != L->next)
    {
        oneNode = oneNode->next;
        i++;
    }
    return i;
}
// 销毁
int list_destroy(linkList *L)
{
    if (*L == NULL)
    {
        printf("链表为空\n");
    }
    linkList oneNode = (*L)->next;
    for (int i = 0; oneNode->next != (*L)->next; i++)
    {
        printf("%d\t", oneNode->data);
        linkList q = oneNode;
        oneNode = oneNode->next;
        free(q);
    }
    free(oneNode);
    free(*L);
    *L = NULL;
    printf("\n");
}
// 头插
int head_insert(linkList L, int value)
{
    printf("头插函数\n");
    // 表头指向的链表不为NULL
    if (L == NULL)
    {
        printf("链表为空\n");
    }
    linkList oneNode = L->next;
    // 分配新节点空间
    linkList newNode = (linkList)malloc(sizeof(linkList));
    newNode->data = value;
    newNode->next = NULL;
    newNode->prior = NULL;
    if (newNode == NULL)
    {
        printf("分配空间失败\n");
    }
    if (oneNode->prior == oneNode->next && oneNode->data == 0)
    {
        oneNode->data = value;
        return 0;
    }
    newNode->prior = oneNode->prior;
    newNode->next = oneNode;
    newNode->prior->next = newNode;
    newNode->next->prior = newNode;
    L->next = newNode;
}
// 尾插
int tail_insert(linkList L, Data_type value)
{
    if (L == NULL)
    {
        printf("链表为空\n");
    }

    linkList newNode = (linkList)malloc(sizeof(Data_type));
    newNode->data = value;
    linkList oneNode = L->next;
    if (oneNode->prior == oneNode->next && oneNode->data == 0)
    {
        oneNode->data = value;
        return 0;
    }
    while (oneNode->next != L->next)
    {
        oneNode = oneNode->next;
    }
    newNode->next = L->next;
    newNode->prior = oneNode;
    newNode->prior->next = newNode;
    newNode->next->prior = newNode;
}
// 修改
int list_change(linkList L, int pos, Data_type value)
{
    if (L == NULL)
    {
        printf("链表为空\n");
        return -1;
    }
    if (pos < 0 || pos > list_length(L))
    {
        printf("位置不合法\n");
        return -1;
    }
    linkList oneNode = L->next;
    int i = 0;
    for (i = 0; i < pos; i++)
    {
        oneNode = oneNode->next;
    }
    oneNode->data = value;
    return 0;
}
// 删除
int list_delete(linkList L, int pos)
{
    if (L == NULL)
    {
        printf("链表为空\n");
    }
    if (pos < 0 || pos > list_length(L))
    {
        printf("位置不合法\n");
        return -1;
    }
    linkList oneNode = L->next;
    for (int i = 0; i < pos; i++)
    {
        oneNode = oneNode->next;
    }
    oneNode->prior->next = oneNode->next;
    oneNode->next->prior = oneNode->prior;
    free(oneNode);
    oneNode = NULL;
}
// 指定位置插入
int list_pos_insert(linkList L, int pos, Data_type value)
{
    printf("指定插入第%d个位置\n", pos);
    if (L == NULL)
    {
        printf("链表为空\n");
        return -1;
    }
    if (pos < 0 || pos > list_length(L))
    {
        printf("位置不合法\n");
        return -1;
    }
    linkList newNode = (linkList)malloc(sizeof(Data_type));
    newNode->data = value;
    linkList oneNode = L->next;
    for (int i = 0; i < pos; i++)
    {
        oneNode = oneNode->next;
    }
    newNode->next = oneNode;
    newNode->prior = oneNode->prior;
    newNode->prior->next = newNode;
    newNode->next->prior = newNode;
    oneNode = newNode;
    return 0;
}
// 合并两个链表
int list_merge(linkList L, linkList L2)
{
    linkList LNode = L->next;
    linkList L2Node = L2->next;
    while (L2Node->next != L2->next)
    {
        printf("%d\n", L2Node->data);
        L2Node = L2Node->next;
        tail_insert(L, L2Node->data);
    }
}
// 翻转
void list_reverse(linkList L)
{
    printf("翻转\n");
    linkList pNode = L->next;
    int i = 0;
    while (i < list_length(L))
    {
        // 1234
        // 4123
        // 4312
        // 4321
        pNode = pNode->next;
        if (i == 0)
        {
            head_insert(L, L->next->prior->data);
        }
        else
        {
            list_pos_insert(L, i, L->next->prior->data);
        }
        list_delete(L, list_length(L) - 1);
        i++;
    }
}
// void list_reverse2(linkList L)
// {
//     linkList arr[list_length(L)];
//     if (L == NULL || L->next == L)
//     {
//         return;
//     }
//     printf("翻转\n");
//     linkList currNode = L->next; // 当前节点
//     int i = 0;
//     while (currNode->next != L->next)
//     {
//         arr[i] = currNode;
//         currNode = currNode->next;
//         i++;
//     }
//     arr[(sizeof(arr) / sizeof(linkList)) - 1] = currNode;
//     int k = 0;
//     for (int j = sizeof(arr) / sizeof(linkList) - 1; j >= 0; j--)
//     {
//         printf("%d,%d\t", k, arr[j]->data);
//         list_change(L, k, arr[j]->data);
//         k++;
//     }
//     printf("\n");
// }
// 打印
void list_show(linkList L)
{
    if (L == NULL)
    {
        printf("链表为空\n");
        return;
    }
    linkList oneNode = L->next;
    while (oneNode->next != L->next)
    {
        printf("%d\t", oneNode->data);
        oneNode = oneNode->next;
    }
    printf("%d\n", oneNode->data);
}
void main()
{
    LNode *tableHead = (LNode *)malloc(sizeof(LNode));
    linkList head = NULL;
    tableHead->next = create_link_list(head);
    // head_insert(tableHead, 6);
    // head_insert(tableHead, 7);
    // head_insert(tableHead, 8);
    // head_insert(tableHead, 9);
    // tail_insert(tableHead, 10);
    // tail_insert(tableHead, 11);
    // tail_insert(tableHead, 12);
    // tail_insert(tableHead, 13);
    // tail_insert(tableHead, 14);
    // list_show(tableHead);
    list_length(tableHead);
    // linkList findValue = list_find(tableHead, 5);
    // printf("%d\n", findValue->data);
    // list_destroy(&tableHead),
    //     list_length(tableHead);
    // list_show(tableHead);
    // list_change(tableHead,2,998);
    // list_delete(tableHead, 2);
    // list_pos_insert(tableHead, 4, 6666);
    LNode *tableHead2 = (LNode *)malloc(sizeof(LNode));
    linkList head2 = NULL;
    tableHead2->next = create_link_list(head2);
    tail_insert(tableHead2, 1);
    tail_insert(tableHead2, 2);
    tail_insert(tableHead2, 3);
    tail_insert(tableHead2, 4);
    tail_insert(tableHead2, 5);
    tail_insert(tableHead2, 6);
    tail_insert(tableHead2, 7);
    list_show(tableHead2);
    list_reverse(tableHead2);

    list_show(tableHead2);
    // list_merge(tableHead, tableHead2);
    // list_show(tableHead);
}